/**
 * @author Burcak Otlu Saritas
 *
 * 
 */

package keggpathway.ncbigenes;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import annotate.encode.Commons;

public class KeggPathway2NcbiGeneIds {
	
	public void readKeggPathwayHsaList(String fileName, Set<KeggPathway> keggPathwaySet ){
		
		String strLine;
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		
		int indexofFirstTab 	= -1;
		int indexofSecondTab 	= -1;
		int indexofFirstColon 		= -1;
		int indexofSecondColon 		= -1;
		
		String keggPathwayName;
		String ncbiGeneId;
		
		List<String> keggPathwayNameList = new ArrayList<String>();
		
		try {
			fileReader = new FileReader(fileName);
			bufferedReader = new BufferedReader(fileReader);
			
			while((strLine = bufferedReader.readLine())!=null){

				indexofFirstTab = strLine.indexOf('\t');
				indexofSecondTab = strLine.indexOf('\t',indexofFirstTab+1);
				
				keggPathwayName = strLine.substring(0,indexofFirstTab);
				ncbiGeneId = strLine.substring(indexofFirstTab+1,indexofSecondTab);
				
				indexofFirstColon = keggPathwayName.indexOf(':');
				indexofSecondColon = ncbiGeneId.indexOf(':');
				
				keggPathwayName = keggPathwayName.substring(indexofFirstColon+1);				
				ncbiGeneId = ncbiGeneId.substring(indexofSecondColon+1);
				
				if(!(keggPathwayNameList.contains(keggPathwayName))){
					
					keggPathwayNameList.add(keggPathwayName);
					
					List<String> geneIdList = new ArrayList<String>();
					
					KeggPathway keggPathway = new KeggPathway(keggPathwayName,geneIdList);
					
					keggPathway.getGeneIdList().add(ncbiGeneId);
					
					keggPathwaySet.add(keggPathway);
					
				} else{
//					Find the already existing keggPathway object by iterating
					Iterator<KeggPathway> itr = keggPathwaySet.iterator();
					
					while(itr.hasNext()){
						KeggPathway keggPathway = (KeggPathway) itr.next();
						if (keggPathway.getKeggPathwayName().equals(keggPathwayName)){
							keggPathway.getGeneIdList().add(ncbiGeneId);
						}
					}
					
				}
				
				
				
				
			} // End of While
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			bufferedReader.close();
			fileReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}					
	}
	
	
	public void writeKeggPathway2NcbiGeneIdFiles(Set<KeggPathway> keggPathwaySet){
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		
		Iterator<KeggPathway> itr = keggPathwaySet.iterator();
		try {

			while(itr.hasNext()){
				KeggPathway keggPathway = (KeggPathway)itr.next();
				fileWriter = new FileWriter(Commons.KEGG_PATHWAY_2_NCBI_GENE_IDS_OUTPUT_FILE_PATH + keggPathway.getKeggPathwayName() + "_to_NcbiGeneList.txt");
				bufferedWriter = new BufferedWriter(fileWriter);
				
				for(int i = 0; i<keggPathway.getGeneIdList().size(); i++){
					bufferedWriter.write(keggPathway.getGeneIdList().get(i)+ "\n");
					bufferedWriter.flush();
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			bufferedWriter.close();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}								
	}
	
	public void writeAllPossibleKeggPathwayNamesFile(Set<KeggPathway> keggPathwaySet){
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		
		Iterator<KeggPathway> itr = keggPathwaySet.iterator();
		try {

			fileWriter = new FileWriter(Commons.ALL_POSSIBLE_KEGG_PATHWAY_NAMES_OUTPUT_FILE);
			bufferedWriter = new BufferedWriter(fileWriter);
		
			while(itr.hasNext()){
				KeggPathway keggPathway = (KeggPathway)itr.next();
								
				bufferedWriter.write(keggPathway.getKeggPathwayName()+ "\n");
				bufferedWriter.flush();				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			bufferedWriter.close();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}									
	}
	
	public static  void main(String[] args){
		
		Set<KeggPathway> keggPathwaySet = new HashSet<KeggPathway>();
		
		KeggPathway2NcbiGeneIds keggPathwat2NcbiGenes = new KeggPathway2NcbiGeneIds();
		keggPathwat2NcbiGenes.readKeggPathwayHsaList(Commons.KEGG_PATHWAY_2_NCBI_GENE_IDS_INPUT_FILE, keggPathwaySet);
		keggPathwat2NcbiGenes.writeKeggPathway2NcbiGeneIdFiles(keggPathwaySet);
		keggPathwat2NcbiGenes.writeAllPossibleKeggPathwayNamesFile(keggPathwaySet);
		
	}

}
